import {createRouter, createWebHashHistory, createWebHistory, Router, RouteRecordRaw} from 'vue-router'
import Promise from '@/router/promise'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: "/",
        redirect: '/home'
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/pages/home/login.vue')
    },
    {
        path: '/home',
        name: 'home',
        component: () => import('@/pages/home/home.vue'),
        meta: {title: '首页'},
        redirect: '/index',
        children: [
            {
                path: '/index',
                name: 'index',
                component: () => import('@/pages/home/index.vue'),
                meta: {title: '工作台'}
            },
            {
                path: '/iframe',
                name: 'iframe',
                component: () => import('@/pages/home/iframe.vue'),
                meta: {title: '工作台'}
            },
        ]
    },
    {
        path: "/404",
        component: () => import('@/pages/home/404.vue')
    },
    {
        path: "/:pathMatch(.*)*",
        redirect: '/404'
    }
]


//动态添加page下的所有页面路由
const modules = import.meta.glob('../pages/**/*.vue');
let propertyNames = Object.getOwnPropertyNames(modules);
for (const propertyName of propertyNames) {
    if (propertyName.indexOf("component") != -1 || propertyName.indexOf("home") != -1) {
        continue;
    }
    let path = propertyName.replaceAll("../pages", "").replaceAll(".vue", "");
    let name = propertyName.replaceAll("../pages/", "").replaceAll(".vue", "");
    let route:RouteRecordRaw = {path:path,name:name, component: modules[propertyName]};
    routes.filter(value => value.name === 'home')[0]?.children?.push(route);
}
const router = createRouter({
    history: createWebHashHistory(),
    routes
})

//路由守卫
Promise(router);

export default router
